<?php

/**
 * Project: Espolon
 * Licence: BSD License
 * Link: http://espolon.org/
 * Copyright: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Author: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Version: Beta
 */

if (!defined('SITE_ROOT')) exit('No direct script access allowed');

class AddModuleGroup extends HTML_QuickForm {
  private $tpl;
  private $mod;

  function __construct($tpl,$mod) {
    $this->tpl = $tpl;
    $this->mdl = $mod;

    if($this->tpl->get_template_vars('modify')) {
      $group = $mod->getGroup($mod->getId());
      $current_modules = $mod->getModules($mod->getId());

      $defaults = array('name' => $group['name'],
                        'description' => $group['description']
                        );

      foreach ($mod->getModules($mod->getId()) as $value) {
        $defaults[$value] = $value;
      }

      $this->setDefaults($defaults);
    }


    $gmodules = $mod->getModules();
    $this->tpl->assign('gmodules', $gmodules);

   # $this->setDefaults(array('status' => 1));

    parent::HTML_QuickForm('addgroup');
    $this->addElement('header', 'header', 'addgroup');
    $this->addElement('text', 'name', _('name'), array('size' => 15, 'maxlength' => 50));
    $this->addElement('text', 'description', _('description'), array('size'=>30, 'maslength'=>250));
    if (!empty($gmodules)) {
      foreach ($gmodules as $value) {
        $checkbox[] = $this->addElement('checkbox', $value['id'], $value['name']);
      }
      $this->addGroup($checkbox, 'checkbox', null);
      $this->addGroupRule('checkbox', 'You must select at least one module.','required', '', 1);
    }
    $this->addElement('submit', 'btn_Submit', _('submit'), array('class' => 'submit', 'onmouseover' => 'this.style.backgroundColor = \'#E0E7E7\'; this.style.borderColor = \'#728FA4\';', 'onmouseout' => 'this.style.backgroundColor = \'#eeeeee\'; this.style.borderColor = \'#999999\';'));
    $this->applyFilter('__ALL__', 'trim');
    $this->registerRule('check_name','function','checkExistsName', $this);
    $this->addRule('name', _('name required'), 'required');
    $this->addRule('name', _('already exists'), 'check_name');
  }

  function checkExistsName() {
    $this->mdl->setName($this->getSubmitValue('name'));
    if($this->tpl->get_template_vars('modify')) {
      return $this->mdl->checkExists($op='modify');
    } else {
      return $this->mdl->checkExists($op='name');
    }
  }

  function displayForm() {
    $this->renderer =& new HTML_QuickForm_Renderer_ArraySmarty($this->tpl);
    $this->renderer->setRequiredTemplate(
  		    '{if $error}
  		 	     <font color="red">{$label}</font>
  	 	     {else}
  	 		     {$label}
  		 	     {if $required}
  	            <font color="red" size="1">*</font>
  	   	     {/if}
  	       {/if}'
    );
    $this->renderer->setErrorTemplate(
          '{if $error}
  	  	    <font color="blue" size="1">{$error}</font><br />
  	       {/if}{$html}'
    );
    $this->setRequiredNote(
   		'<small class="red">*&nbsp;</small><small class="blue">'._('required fields').'</small>'
   	);
   	$this->accept($this->renderer);
   	$this->tpl->assign('form_data',$this->renderer->toArray());
   	$this->tpl->assign('msg',$this->mdl->getMsg());
  }

  function process() {
    if($this->tpl->get_template_vars('modify')) {
      $this->mdl->modifyGroup($this->getSubmitValues());
    } else {
      $this->mdl->addGroup($this->getSubmitValues());
    }
    $this->tpl->assign('result',true);
    $this->tpl->assign('msg',$this->mdl->getMsg());
  }

} // end of class

?>